const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const router = require('./router')
const errorHandler = require('./middleware/error-handler')
const notFoundHandler = require('./middleware/not-found-handler')

const app = express()
app.use(morgan('dev'))
app.use(cors()) // 允许跨域请求
app.use(express.json())
app.use(express.urlencoded({extended: false}))

const PORT = process.env.PORT || 3000

app.use('/api', router)

// 404处理
app.use(notFoundHandler())

// 挂载统一处理服务端错误中间件
app.use(errorHandler())

app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}/`)
})